var nxtApp = angular.module('NxtMap'); 
nxtApp.directive("nxtTable", function () {  
  return {  
    restrict: 'AE',  
    transclude: true,  
    template: '<div class="info-table" ng-transclude></div>',
  };
});

nxtApp.directive("nxtTableTitle", function () {  
  return {
    require: '^nxtTable',  
    restrict: 'AE',  
    transclude: true,  
    template: '<div class="table-title" ng-transclude></div>'  
  };  
});  

nxtApp.directive("nxtTableList", function () {  
  return {  
    require: '^nxtTable',  
    restrict: 'AE',  
    transclude: true,  
    template: '<div class="table-list" ng-transclude></div>'  
  };  
});

nxtApp.directive("nxtTablePagination", function () {  
  return {  
    require: '^nxtTable',  
    restrict: 'AE',  
    transclude: true,  
    templateUrl: '../../templates/directive/map-table-pagination.html',
    scope: {
        title: '@',
        icon: '@'
    },
    link : function(scope, element, attrs) {

      scope.goPage = function (value) {
        if(value == 'pre') {
          if(scope.tparam.now > 1) {
            scope.tparam.now --;
          }
        }
        else if(value == 'next') {
          if(scope.tparam.now < $scope.tparam.max){
            scope.tparam.now ++;
          }
        }
        else if(value == '···' || value == '··') {
          return;
        }
        else {
          scope.tparam.now = value;
        }
        scope.tparam.pages = generatePageNum(scope.tparam.now, scope.tparam.max);
      }

      function generatePageNum(index, max) {
        var result = [];
        var num = 5;
        if(max < 8){
          var ii = 1;
          while (ii <= max) {
            result.push(ii);
            ii++;
          }
          return result;
        }
        var minv = index - 2, maxv = index + 2;
        if(minv <= 1) {
          maxv = 1 - minv + maxv;
          minv = 1;
        }
        else {
          result.push('··');
        }
        if(maxv >= max) {
          minv = minv - (maxv - max);
          maxv = max;     
        }
        for(var i = minv; i <= maxv; i++){
          result.push(i);
        }
        if(maxv < max) {
          if(maxv < max - 1){
            result.push('···');
          }
          result.push(max);
        }
        return result;
      }
    } 
  };  
});
